<template>
  <div class="teacher-list-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <nav class="nav-container">
        <div class="nav-left">
          <button class="back-btn" @click="goBack">
            <span class="back-icon">←</span>
            返回
          </button>
          <h1 class="page-title">教师队伍</h1>
        </div>
        <div class="nav-right">
          <button class="user-btn" @click="handleProfileClick">
            <span class="user-icon">👤</span>我的
          </button>
        </div>
      </nav>
    </header>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="container">
        <!-- 搜索功能 -->
        <div class="search-section">
          <div class="search-container">
            <input 
              v-model="searchKeyword" 
              type="text" 
              placeholder="可以搜索教师姓名、工号"
              class="search-input"
              @input="handleSearch"
              @keyup.enter="handleSearch"
            />
            <button class="search-btn" @click="handleSearch">🔍</button>
          </div>
        </div>

        <!-- 教师列表 -->
        <div class="teacher-list">
          <div class="list-header">
            <div class="header-item">工号</div>
            <div class="header-item">姓名</div>
            <div class="header-item">专业职称</div>
            <div class="header-item">联系电话</div>
          </div>
          
          <!-- 加载状态 -->
          <div v-if="loading" class="loading-container">
            <div class="loading-spinner"></div>
            <div class="loading-text">正在加载教师信息...</div>
          </div>

          <!-- 教师列表项 -->
          <div v-else class="teacher-items">
            <div 
              v-for="teacher in displayTeachers" 
              :key="teacher.id || teacher.teacherId"
              class="teacher-item"
              @click="handleTeacherClick(teacher)"
            >
              <div class="teacher-info">
                <span class="teacher-employee-id">{{ teacher.employeeId }}</span>
              </div>
              <div class="teacher-info">
                <span class="teacher-name">{{ teacher.realName }}</span>
              </div>
              <div class="teacher-info">
                <span class="teacher-title">{{ teacher.professionalTitle || '暂无' }}</span>
              </div>
              <div class="teacher-info">
                <span class="teacher-phone">{{ teacher.phone || '暂无' }}</span>
              </div>
            </div>
          </div>

          <!-- 空状态 -->
          <div v-if="!loading && displayTeachers.length === 0" class="empty-state">
            <div class="empty-icon">👨‍🏫</div>
            <div class="empty-text">暂无符合条件的教师</div>
            <div class="empty-hint">请尝试调整搜索条件</div>
          </div>
        </div>
      </div>
    </main>

    <!-- 消息提示弹窗 -->
    <div v-if="showMessageModal" class="modal-overlay" @click="closeMessageModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header error-header">
          <h3>加载失败</h3>
          <button class="close-btn" @click="closeMessageModal">×</button>
        </div>
        <div class="modal-body">
          <div class="icon-wrapper">
            <div class="message-icon error-icon">✗</div>
          </div>
          <p class="modal-text">{{ errorMessage }}</p>
        </div>
        <div class="modal-footer">
          <button class="confirm-btn" @click="closeMessageModal">确定</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useTokenStore } from '@/stores/token.js'
import { getAllTeachersService, searchTeachersService } from '@/api/teacher.js'

const router = useRouter()
const tokenStore = useTokenStore()

// 响应式数据
const searchKeyword = ref('') // 搜索关键词
const loading = ref(false) // 加载状态
const showMessageModal = ref(false) // 消息提示弹窗
const errorMessage = ref('') // 错误信息

// 教师数据（从API获取）
const teachers = ref([])
const searchResults = ref([])

// 计算属性：显示的教师列表
const displayTeachers = computed(() => {
  if (searchKeyword.value.trim()) {
    return searchResults.value
  }
  return teachers.value
})

// 处理搜索
const handleSearch = async () => {
  const keyword = searchKeyword.value.trim()
  
  if (!keyword) {
    // 如果搜索关键词为空，显示所有教师
    searchResults.value = []
    return
  }

  loading.value = true
  try {
    console.log('搜索教师，关键词:', keyword)
    const result = await searchTeachersService(keyword)
    console.log('搜索教师API响应:', result)
    
    if (result.code === 200) {
      searchResults.value = result.data || []
      console.log('搜索教师成功:', searchResults.value.length, '位教师')
    } else {
      console.error('搜索教师失败:', result.msg)
      showErrorMessage(result.msg || '搜索教师失败')
      searchResults.value = []
    }
  } catch (error) {
    console.error('搜索教师异常:', error)
    showErrorMessage('搜索教师时发生错误')
    searchResults.value = []
  } finally {
    loading.value = false
  }
}

// 页面挂载时的初始化
onMounted(async () => {
  // 检查登录状态
  if (!tokenStore.token) {
    router.push('/login')
    return
  }
  
  await loadTeachers()
})

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 处理个人中心点击
const handleProfileClick = () => {
  router.push('/profile')
}

// 加载教师数据
const loadTeachers = async () => {
  loading.value = true
  try {
    console.log('开始加载教师数据...')
    const result = await getAllTeachersService()
    console.log('教师数据API响应:', result)
    
    if (result.code === 200) {
      teachers.value = result.data || []
      console.log('教师数据加载成功:', teachers.value.length, '位教师')
    } else {
      console.error('加载教师数据失败:', result.msg)
      showErrorMessage(result.msg || '加载教师数据失败')
    }
  } catch (error) {
    console.error('加载教师数据异常:', error)
    showErrorMessage('加载教师数据时发生错误')
  } finally {
    loading.value = false
  }
}

// 点击教师项
const handleTeacherClick = (teacher) => {
  console.log('点击教师:', teacher)
  
  const employeeId = teacher.employeeId
  
  if (!employeeId) {
    showErrorMessage('教师工号信息缺失')
    return
  }
  
  // 跳转到教师详情页面
  router.push({
    name: 'TeacherDetail',
    params: { employeeId: employeeId }
  })
}

// 显示错误消息
const showErrorMessage = (text) => {
  errorMessage.value = text
  showMessageModal.value = true
}

// 关闭消息弹窗
const closeMessageModal = () => {
  showMessageModal.value = false
}
</script>

<style scoped>
.teacher-list-container {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 顶部导航栏 */
.header {
  background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
  padding: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
}

.nav-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.back-icon {
  font-size: 16px;
}

.page-title {
  color: white;
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-btn {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 8px 20px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.user-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.user-icon {
  font-size: 16px;
  opacity: 0.9;
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
}

.container {
  width: 100%;
}

/* 搜索功能 */
.search-section {
  margin-bottom: 30px;
}

.search-container {
  display: flex;
  gap: 15px;
  background: white;
  border-radius: 15px;
  padding: 25px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.search-input {
  flex: 1;
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 8px;
  font-size: 16px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  border-color: #6278db;
}

.search-btn {
  padding: 12px 20px;
  background: #6278db;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 16px;
}

.search-btn:hover {
  background: #5a6fd8;
  transform: translateY(-1px);
}

/* 教师列表 */
.teacher-list {
  background: white;
  border-radius: 15px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.list-header {
  display: grid;
  grid-template-columns: 1.5fr 1.5fr 2fr 2fr;
  gap: 15px;
  padding: 20px 25px;
  background: #f8f9fa;
  border-bottom: 1px solid #e1e5e9;
}

.header-item {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  text-align: center;
}

.teacher-item {
  display: grid;
  grid-template-columns: 1.5fr 1.5fr 2fr 2fr;
  gap: 15px;
  padding: 20px 25px;
  border-bottom: 1px solid #f0f0f0;
  transition: all 0.3s ease;
  align-items: center;
  cursor: pointer;
}

.teacher-item:hover {
  background: #f8f9fa;
  transform: translateY(-1px);
}

.teacher-item:last-child {
  border-bottom: none;
}

.teacher-info {
  font-size: 16px;
  color: #333;
  text-align: center;
}

.teacher-name {
  font-weight: 600;
  color: #2c3e50;
}

.teacher-employee-id {
  font-family: 'Courier New', monospace;
  color: #6278db;
  font-weight: 500;
}

.teacher-title {
  color: #27ae60;
  font-weight: 500;
}

.teacher-phone {
  color: #e67e22;
  font-family: 'Courier New', monospace;
}

/* 加载状态 */
.loading-container {
  padding: 60px 20px;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #6278db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  font-size: 16px;
  color: #666;
  font-style: italic;
}

/* 空状态 */
.empty-state {
  padding: 60px 20px;
  text-align: center;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
  opacity: 0.6;
}

.empty-text {
  font-size: 20px;
  color: #333;
  font-weight: 500;
  margin-bottom: 10px;
}

.empty-hint {
  font-size: 16px;
  color: #666;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease-out;
}

.modal-content {
  background: white;
  border-radius: 20px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.3s ease-out;
  position: relative;
  overflow: hidden;
}

.modal-header {
  background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
  color: white;
  padding: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.3s ease;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-body {
  padding: 30px 20px;
  text-align: center;
}

.icon-wrapper {
  margin-bottom: 20px;
}

.message-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 30px;
  font-weight: bold;
  margin: 0 auto;
  background: linear-gradient(135deg, #dc3545 0%, #fd7e14 100%);
}

.modal-text {
  font-size: 18px;
  color: #333;
  margin: 0 0 10px 0;
  font-weight: 600;
}

.modal-footer {
  padding: 0 20px 20px;
  display: flex;
  gap: 12px;
  justify-content: center;
}

.confirm-btn {
  flex: 1;
  max-width: 120px;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #6278db;
  color: white;
  border: none;
}

.confirm-btn:hover {
  background: #5a6fd8;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(98, 120, 219, 0.3);
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: scale(0.7);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-container {
    padding: 10px 15px;
  }
  
  .nav-left {
    gap: 15px;
  }
  
  .page-title {
    font-size: 18px;
  }
  
  .main-content {
    padding: 20px 15px;
  }
  
  .search-container {
    padding: 20px;
  }
  
  .list-header,
  .teacher-item {
    grid-template-columns: 1.2fr 1.2fr 1.5fr 1.5fr;
    gap: 10px;
    padding: 15px 20px;
  }
  
  .header-item,
  .teacher-info {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .nav-left {
    gap: 10px;
  }
  
  .back-btn {
    padding: 6px 12px;
    font-size: 13px;
  }
  
  .page-title {
    font-size: 16px;
  }
  
  .search-container {
    padding: 15px;
    gap: 10px;
  }
  
  .search-input {
    font-size: 14px;
    padding: 10px 12px;
  }
  
  .search-btn {
    padding: 10px 15px;
    font-size: 14px;
  }
  
  .list-header,
  .teacher-item {
    grid-template-columns: 1fr 1fr 1fr 1fr;
    gap: 8px;
    padding: 12px 15px;
  }
  
  .header-item,
  .teacher-info {
    font-size: 12px;
  }
  
  .modal-content {
    width: 95%;
    margin: 20px;
  }
  
  .modal-header {
    padding: 15px;
  }
  
  .modal-header h3 {
    font-size: 16px;
  }
  
  .modal-body {
    padding: 25px 15px;
  }
  
  .modal-text {
    font-size: 16px;
  }
  
  .confirm-btn {
    padding: 10px 15px;
    font-size: 14px;
  }
}
</style>
